﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BillGuru
{
    public partial class FrmInvoiceHistory : Form
    {
        public FrmInvoiceHistory()
        {
            InitializeComponent();
        }

        private void FrmInvoiceHistory_Load(object sender, EventArgs e)
        {
            vendorTableAdapter.Fill(billGuruDataSet.Vendor);
            customerTableAdapter.Fill(billGuruDataSet.Customer);
            invoiceTypeTableAdapter.Fill(billGuruDataSet.InvoiceType);
            companyTableAdapter.Fill(billGuruDataSet.Company);
        }

        private void pbSearch_Click(object sender, EventArgs e)
        {
            string sFilter;

            sFilter = "InvoiceDate >= #" + dtpStartDate.Value.Date.ToShortDateString() + "# and InvoiceDate <= #" + dtpEndDate.Value.Date.ToShortDateString() + "# ";
            //sFilter = "InvoiceDate > " + "2009-1-1";// + "'// and InvoiceDate < '" + "2009-12-12" + "' ";
            if (ckCustomer.Checked)
                sFilter += " and CustomerID = " + cbCustomer.SelectedValue + " ";
            if(ckVendor.Checked)
                sFilter += " and VendorID = " + cbVendor.SelectedValue + " ";
            if (ckCompany.Checked)
                sFilter += " and CompanyID = " + cbCompany.SelectedValue + " ";

            string sCommand = "Select * from Invoice where " + sFilter;

            System.Data.OleDb.OleDbDataAdapter invoiceTableAdapter = new System.Data.OleDb.OleDbDataAdapter();
            invoiceTableAdapter.SelectCommand = new System.Data.OleDb.OleDbCommand(sCommand, new System.Data.OleDb.OleDbConnection(BillGuru.Properties.Settings.Default.BillGuruConnectionString));

            billGuruDataSet.Invoice.Clear();
            invoiceTableAdapter.Fill(billGuruDataSet.Invoice);
        }

        private void invoiceDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int nInvoiceID = (int)billGuruDataSet.Invoice.Rows[e.RowIndex]["ID"];

            FrmInvoice frmInvoice = new FrmInvoice();
            //frmInvoice.LoadInvoice(nInvoiceID);
            frmInvoice.InvoiceId = nInvoiceID;

            frmInvoice.Show();

        }

        private void pbDone_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmInvoiceHistory_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.billGuruDataSet.HasChanges())
                BillGuru.UI.PermissionHandler.checkDataLoss(e);
        }
    }
}
